﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebApplication2.Class;

namespace WebApplication2
{
    public partial class WeightSelect : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string  patientid = "";
            patientid = (string)Session["PatientID"];
            tbxPatientID.Text = patientid;

        }
        protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == System.Web.UI.WebControls.DataControlRowType.DataRow)
            {

                // when mouse is over the row, save original color to new attribute, and change it to highlight color
                e.Row.Attributes.Add("onmouseover", "this.originalstyle=this.style.backgroundColor;this.style.backgroundColor='#FFCCCC'");

                // when mouse leaves the row, change the bg color to its original value  
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=this.originalstyle;");
                e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.GridView2, "Select$" + e.Row.DataItemIndex, true);
            }
        }

          protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
         {
             
                 int rowindex = GridView2.SelectedIndex;
                 string value = GridView2.Rows[rowindex].Cells[2].Text.ToString();
                 // Session["weight"] = weight;
                 // ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('"+ weight + "');", true);
                 //  
                 ScriptManager.RegisterStartupScript(this, this.GetType(), "windowScript", "submitData(" + value + ");", true);
                 //ClientScript.RegisterStartupScript(typeof(Page), "closePage", "window.close();", true);
             
            
         }

          protected void lblSearch_Click(object sender, EventArgs e)
          {
              string patientid = tbxPatientID.Text;
              string startdate = Sdate.Value + " " +  Sime1.Value;
              string enddate = SDate2.Value + " " + Etime.Value;


              string s = Convert.ToDateTime(startdate).ToString("yyyy-MM-dd hh:mm:ss");
              string startDate = s;

              string end = Convert.ToDateTime(enddate).ToString("yyyy-MM-dd hh:mm:ss");
              string endDate = end;


             WebRequest req = WebRequest.Create("http://192.168.1.156/api/FMTXWeighingData?patientid=" + patientid + "&&datetime=" + startDate + "&&datetime1= " + endDate);
             req.ContentType = "application/json";
             WebResponse resp = req.GetResponse();
             Stream stream = resp.GetResponseStream();
             StreamReader re = new StreamReader(stream);
             String json = re.ReadToEnd();
            //// json = "{\"FMTXWeighingData\":" + json + "}";

            JavaScriptSerializer ser = new JavaScriptSerializer();
            List<FMTXWeighingData> keysWrapper = Newtonsoft.Json.JsonConvert.DeserializeObject<List<FMTXWeighingData>>(json);

           //  keywrapperWeighing keysWrapper = JsonConvert.DeserializeObject<keywrapperWeighing>(json);

            try
            {
                
                List<FMTXWeighingData> weighingData = new List<FMTXWeighingData>();
                JArray v = JArray.Parse(json);
                for (int i = 0; i < keysWrapper.Count; i++)
                {
                    //var obj = JObject.Parse(json);

                    FMTXWeighingData data = new FMTXWeighingData();
                    //data.Date_Created = Convert.ToDateTime((obj["Date_Created"]));
                    //data.MachineID = (obj["MachineID"]).ToString();
                    //data.Weight = Convert.ToDecimal(obj["Weight"]);
                    //data.isDesiredWeight = Convert.ToBoolean(obj["isDesiredWeight"]);

                   
                    data.Date_Created = Convert.ToDateTime( v[i]["Date_Created"].ToString());
                    data.MachineID = v[i]["MachineID"].ToString();

                    data.Weight =Convert.ToDecimal( v[i]["Weight"].ToString());
                    data.isDesiredWeight = Convert.ToBoolean(v[i]["isDesiredWeight"].ToString());


                    weighingData.Add(data);
                }
               
                   
               

                GridView2.DataSource = weighingData;
                GridView2.DataBind();
            }
            catch (Exception)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('No data');", true);

            }


        

             
             
             


          }
    }
}